package com.Lixiaoqian.CardPlay.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.Lixiaoqian.CardPlay.R;
import com.Lixiaoqian.CardPlay.base.BaseActivity;
import com.Lixiaoqian.CardPlay.utils.Constant;
import com.Lixiaoqian.CardPlay.utils.TopSnackbarUtil;

import butterknife.BindView;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import static android.R.attr.launchMode;


/**
 *
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener{


    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_yanzheng)
    EditText editYanzheng;
    @BindView(R.id.btn_yanzheng)
    TextView btnYanzheng;
    @BindView(R.id.edit_mima)
    EditText editMima;
    @BindView(R.id.btn_regist)
    Button btnRegist;
    private Handler handler;
    private boolean isgetpassView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIntent();
        if (isgetpassView){
            btnRegist.setText("重置密码");
        }
        initEvent();
        initHandler();

    }

    private void initIntent() {
        Intent intent = getIntent();
        isgetpassView = intent.getBooleanExtra(Constant.ISGETPASS_ACTIVITY,false);
    }

    private void initEvent() {
        btnYanzheng.setOnClickListener(this);
        btnRegist.setOnClickListener(this);

        editPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length()>=11&&!runningDownTimer){
                    btnYanzheng.setEnabled(true);
                }else {
                    btnYanzheng.setEnabled(false);

                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initToolbar() {
        super.initToolbar();
        boolean b = getIntent().getBooleanExtra(Constant.ISGETPASS_ACTIVITY, false);
        if (b) {
            actionBar.setTitle("重置密码");
        }else{
            actionBar.setTitle("注 册");
        }
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_register;
    }

    public static void launch(Activity fromActivity,boolean isgetpassView){
        Intent intent = new Intent(fromActivity, RegisterActivity.class);
        intent.putExtra(Constant.ISGETPASS_ACTIVITY,isgetpassView);
        fromActivity.startActivity(intent);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_yanzheng:
                getYanZhengma();
                break;
            case R.id.btn_regist:
                register();
                break;
        }
    }

    public void getYanZhengma(){
        SMSSDK.getVerificationCode("+86", editPhone.getText().toString());
        CountdownTimer(btnYanzheng);  // 倒计时开始
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eh);
    }

    private void initHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    //验证码验证成功
                    if (TextUtils.isEmpty(editMima.getText())) {
                        TopSnackbarUtil.showTopBar( "请填写密码");
                    } else {
                        //注册到服务器
                        registerService();
                    }
                } else {
                    // 验证码验证失败
                    TopSnackbarUtil.showTopBar("验证码错误");
                }
            }
        };
    }
    /**
     * 注册_验证-验证码状态并Handler发送
     */
    public void register() {
        SMSSDK.submitVerificationCode("+86", editPhone.getText().toString().trim(), editYanzheng.getText().toString().trim());
        eh = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                } else {
                    handler.sendEmptyMessage(0);
                }

            }
        };
        SMSSDK.registerEventHandler(eh);
    }

    /**
     * 注册到服务器
     */
    private void  registerService(){
        //分两种一个注册,一个是重置密码
        if (isgetpassView){
            //重置密码
        }else {
            //注册密码
        }
    }

}
